﻿<?php
class Model_Order extends Zend_Db_Table_Abstract
{
	protected $_name = 'order';
	protected $_primary = 'o_id';

	public function createOrder ($u_id,$pay_id,$ship_id,$os_id,$ps_id)
	{
		$rowOrder= $this->createRow();
		if ($rowOrder) {
			// update the row values
			$rowOrder->u_id = $u_id;
			$rowOrder->pay_id = $pay_id;
			$rowOrder->ship_id = $ship_id;				
			$rowOrder->os_id = $os_id;
			$rowOrder->ps_id = $ps_id;
			//$rowOrder->o_ngaydat = $o_ngaydat;			
			$rowOrder->save();			
			//return the new Order			
			return $rowOrder;
		} else {
			throw new Zend_Exception("Không thể tạo đơn hàng !");
		}
	}
		
	public static function getOrderById($o_id)
	{
		$orderModel = new self();
		$select = $orderModel->select();
		$select->order(array('o_id'))
			   ->where('o_id =?',$o_id);	
		return $orderModel->fetchAll($select);
	}
	public static function getOrderByUid($u_id)
	{
		$orderModel = new self();
		$select = $orderModel->select();
		$select->order(array('o_id'))
			   ->where('u_id =?',$u_id);	
		return $orderModel->fetchAll($select);
	}
	
	
	public static function getOrderAll()
	{
		$orderModel = new self();
		$select = $orderModel->select();
		$select->order(array('o_id'));	
		return $orderModel->fetchAll($select);
	}	
    public function fetchLatest($count =6 )
    {
        return $this->fetchAll(null, 
            'o_id DESC', $count);
    }
	
	public static function getOrderLimit($start,$count)
	{
		$orderModel = new self();
		$select = $orderModel->select();
		$select->order(array('o_id'))
			   ->limit($start,$count);	
		return $orderModel->fetchAll($select);
	}
		
	public function deleteOrderById($oid)
	{
		$orderModel = new self();
		$orderDetailModel = new Model_Orderdetail();
		$orderDetailModel->deleteODetailByOrderId($oid);
		$where = $orderModel->getAdapter()->quoteInto('o_id = ?', $oid);
		if($where){
			$orderModel->delete($where);
		}
		else{
			throw new Zend_Exception("Could not delete order");
		}
	}
	
}